package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@Component
//自动填充类
public class MyMetaObjectHandler implements MetaObjectHandler {
    //注入session对象
@Autowired
HttpSession session;
       //添加
    @Override
    public void insertFill( MetaObject metaObject) {
        log.info("start insert fill ....");
        // Long eid = (Long) session.getAttribute("Employee");

        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "createUser", () -> BaseContext.getCurrentId(), Long.class); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "updateUser", () -> BaseContext.getCurrentId(), Long.class); // 起始版本 3.3.3(推荐)

    }

    @Override
    public void updateFill( MetaObject metaObject) {
        log.info("start update fill ....");
       // Long eid = (Long) session.getAttribute("Employee");

        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateUser", () ->BaseContext.getCurrentId(), Long.class); // 起始版本 3.3.3(推荐)
    }
}